package com.blb.bulletscreen;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpRequestDecoder;
import io.netty.handler.codec.http.HttpResponseEncoder;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler;
import io.netty.handler.stream.ChunkedWriteHandler;

import java.net.InetAddress;

/**
 * 弹幕服务器
 */
public class BulletScreenServer {

    public static final int PORT = 8888;

    /**
     * 启动服务器
     */
    public void start(){
        System.out.println("启动弹幕");
        //创建boss线程组
        NioEventLoopGroup bossGroup = new NioEventLoopGroup(2);
        //创建worker线程组
        NioEventLoopGroup workGroup = new NioEventLoopGroup(10);
        try {
            //创建启动对象
            ServerBootstrap serverBootstrap = new ServerBootstrap();
            //配置服务器
            serverBootstrap.group(bossGroup,workGroup)                      //配线程组
                    .channel(NioServerSocketChannel.class)          //通道类型
                    .option(ChannelOption.SO_BACKLOG,1024)       //队列长度
                    .childOption(ChannelOption.SO_KEEPALIVE,true)    //保活
                    .childHandler(new ChannelInitializer<SocketChannel>() {
                        @Override
                        protected void initChannel(SocketChannel ch) throws Exception {
                            //配置管道的处理器链
                            ChannelPipeline pipeline = ch.pipeline();
                            //http解码器
                            pipeline.addLast("http-decodec",new HttpRequestDecoder());
                            //http聚合器
                            pipeline.addLast("http-aggregator",new HttpObjectAggregator(65536));
                            //http编码器
                            pipeline.addLast("http-encodec",new HttpResponseEncoder());
                            //处理大数据块的处理器
                            pipeline.addLast("http-chunked",new ChunkedWriteHandler());
                            //websocket协议的处理器 /ws是路径
                            pipeline.addLast("WebSocket-protocol",new WebSocketServerProtocolHandler("/ws"));
                            //自定义ws请求处理器
                            pipeline.addLast("WebSocket-request",new com.blb.bulletscreen.BulletScreenServerHandler());
                        }
                    });
            //绑定端口
            ChannelFuture future = serverBootstrap.bind(PORT).sync();
            //关闭通道
            future.channel().closeFuture().sync();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            bossGroup.shutdownGracefully();
            workGroup.shutdownGracefully();
        }
    }

    public static void main(String[] args) {
        new BulletScreenServer().start();
    }
}
